package com.wltt.product.frfront.config;

import com.wltt.product.frfront.config.exception.RobotException;
import com.wltt.product.frfront.config.robot.WlttRobot;
import com.wltt.product.frfront.holder.RobotHolder;
import com.wltt.product.frfront.mapper.RobotLogMapper;
import com.wltt.product.frfront.mapper.RobotMapper;
import com.wltt.product.frfront.model.po.RobotPO;
import fairino.FrLogLevel;
import fairino.FrLogType;
import jakarta.annotation.Resource;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RobotConnectConfig {

    private static final Logger logger = LogManager.getLogger(RobotConnectConfig.class);

    @Resource
    private WlttRobot robot;

    @Bean
    public Boolean robotConnect() {
        Thread thread = new Thread(() -> {
            while (true) {
                try {
                    if (robot.isConnected()) {
                        RobotHolder.setRobotConnected(false);
                        robot.CloseRPC();
                        int rtn = robot.connectToRobot();
                        if (rtn == 0) {
                            RobotHolder.setRobotConnected(true);
                        }
                    } else {
                        RobotHolder.setRobotConnected(true);
                    }
                    Thread.sleep(1000);
                } catch (Exception e) {
                }
            }
        });
        thread.start();
        return true;
    }
}
